<?php
// @formatter:off
/**
 * @file getusername.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: getusername.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/users/getusername.php $
 */
// @formatter:on

global $wcPaths;

require_once __DIR__."/tools.php";

class WCServiceUsersGetusername extends WCCaptchaService {
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_requiredParams["url"][] = "user";

		$this->_requiredParams["params"][] = "email";

		$this->_requiredPermissions["to"][] = WC_PERM_USER_LOGIN;
	}
	//
	// Protected methods.
	protected function load() {
		$out = false;

		if(parent::load()) {
			$this->_email = isset($this->email) ? strtolower(trim($this->email)) : false;

			$out = true;
		}

		return $out;
	}
	protected function runPOST() {
		global $wcUsersHolder;

		$user = $wcUsersHolder->userByEmail($this->email);
		if($user && $user->ok()) {
			global $wcAction;
			global $wcThemeAssigns;
			global $wcTheme;

			define("__EMAIL__", true);
			$wcAction = WC_EACTION_USERNAME;
			$wcThemeAssigns["TITLE"] = "Username Request";
			$wcThemeAssigns["USERID"] = $user->id;
			$wcThemeAssigns["USERNAME"] = $user->name();

			startTheme();

			$parser = $wcTheme->parser();
			$parser->massiveAssign($wcThemeAssigns);
			$parser->noWarnings();
			$parser->noSummary();

			if(!SendEmail($this->_email, "Username Request", $parser->parse())) {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "Unable to send e-mail to '{$this->_email}'");
			} else {
				$this->_count++;
			}
		} else {
			$this->setError(WC_HTTPERROR_BAD_REQUEST, "There is no user associated with the e-mail '{$this->_email}'");
		}
	}
}
